import Api from '../../constants/Api'

export default {
  namespaced: true,
  state: {
    messageList: []
  },
  mutations: {
    messageListTo(state, data) {
      state.messageList = data
    }
  },
  actions: {
    getMessageList({
      commit
    }, that) {
      that
        .$axios({
          method: 'POST',
          url: Api.queryMessageList,
          data: {
            current: that.current,
            pageSize: that.pageSize,
            title: that.title,
            readFlag: that.readFlag,
            msgType: that.msgType
          }
        })
        .then(res => {
          if (res.code === 20001) {
            that.total = res.data.total
            commit('messageListTo', res.data.records)
            that.tableLoading = false
            that.setSelectRow()
          } else {
            that.$message.error(res.msg)
          }
        })
    },
    delMessage({
      commit
    }, that) {
      that
        .$axios({
          method: 'delete',
          url: Api.delMessage.replace('{msgId}', that.msgId)
        })
        .then(res => {
          if (res.code === 20004) {
            that.$message.success(res.msg)
            that.getData()
            that.delVisible = false
          } else {
            that.$message.error(res.msg)
          }
        })
    },
    delArrMessage({
      commit
    }, that) {
      that
        .$axios({
          method: 'post',
          url: Api.delArrMessage,
          data: {
            msgIds: that.Ids
          }
        })
        .then(res => {
          if (res.code === 20004) {
            that.$message.success(res.msg)
            that.getData()
            that.delVisible = false
          } else {
            that.$message.error(res.msg)
          }
        })
    },
    readMessage({
      commit
    }, that) {
      that
        .$axios({
          method: 'get',
          url: Api.readMessage,
          params: {
            msgId: that.msgId
          }
        })
        .then(res => {
          if (res.code === 20000) {
            return true
          } else {
            that.$message.error(res.msg)
          }
        })
    }
  }
}